package com.lin.servicehosp.controller.api;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lin.servicehosp.service.DepartmentService;
import com.lin.servicehosp.service.HospitalService;
import com.lin.servicehosp.service.HospitalSetService;
import com.lin.servicehosp.service.ScheduleService;
import com.lin.yygh.common.exception.YyghException;
import com.lin.yygh.common.helper.HttpRequestHelper;
import com.lin.yygh.common.result.Result;
import com.lin.yygh.common.result.ResultCodeEnum;
import com.lin.yygh.common.utils.MD5;
import lin.yygh.model.hosp.Department;
import lin.yygh.model.hosp.Hospital;
import lin.yygh.model.hosp.Schedule;
import lin.yygh.vo.hosp.DepartmentQueryVo;
import lin.yygh.vo.hosp.DepartmentVo;
import lin.yygh.vo.hosp.ScheduleQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.management.Query;
import javax.servlet.http.HttpServletRequest;
import javax.xml.ws.Action;
import java.util.Map;

@RestController
@RequestMapping("/api/hosp")
//@CrossOrigin
public class ApiController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    @PostMapping("hosp/saveHospital")
    public Result saveHosp(HttpServletRequest request){

    //获取到传递过来的医院信息。
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //1、获取医院系统传递过来的签名
        String hospSign = (String) paramMap.get("sign");

        //2、根据传递过来的医院编码，查询数据库，查询签名.
        String hoscode = (String)paramMap.get("hoscode");

        String signkey = hospitalSetService.getSignKey(hoscode);

        //3、把数据库查询签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signkey);

        if (!signKeyMd5.equals(hospSign)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //传输过程中 "+" 转换为了" ",因此我们要转换回来。
        String logoData = (String) paramMap.get("logoData");
        logoData = logoData.replaceAll(" ","+");
        paramMap.put("logoData",logoData);

        //调用service的方法
        hospitalService.save(paramMap);
        return Result.ok();

    }

    //查询医院
    @PostMapping("/hospital/show")
    public Result getHospital(HttpServletRequest request){

        //获取到传递过来的医院信息。
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //获取传递过来的医院编号
        String hoscode = (String) paramMap.get("hoscode");

        //1、获取医院系统传递过来的签名
        String hospSign = (String) paramMap.get("sign");

        String signkey = hospitalSetService.getSignKey(hoscode);

        //2、把数据库查询签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signkey);

        if (!signKeyMd5.equals(hospSign)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //调用service方法实现根据医院编号查询
        Hospital hospital = hospitalService.getByHoscode(hoscode);

        return Result.ok(hospital);
    }

    //上传科室接口
    @PostMapping("/saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        //获取到传递过来的科室信息。
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //1、获取医院系统传递过来的签名
        String hospSign = (String) paramMap.get("sign");

        //2、根据传递过来的医院编码，查询数据库，查询签名.
        String hoscode = (String)paramMap.get("hoscode");

        String signkey = hospitalSetService.getSignKey(hoscode);

        //3、把数据库查询签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signkey);

        if (!signKeyMd5.equals(hospSign)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //调用service的方法
        departmentService.save(paramMap);

        return Result.ok();
    }

    //查询科室接口
    @PostMapping("department/list")
    public Result findDepartment(HttpServletRequest request){

        //获取到传递过来的科室信息。
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //医院信息
        String hoscode = (String) paramMap.get("hoscode");
        int page = StringUtils.isEmpty(paramMap.get("page"))?1:Integer.parseInt((String)paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit"))?1:Integer.parseInt((String)paramMap.get("limit"));
        String hospSign = (String) paramMap.get("sign");

        String signKey = hospitalSetService.getSignKey(hoscode);

        String signKeyMD5 = MD5.encrypt(signKey);

        if (!signKeyMD5.equals(hospSign)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);

//        Department departmentQueryVo = new Department();
        departmentQueryVo.setHoscode(hoscode);

        //调用service方法
        Page<Department> pageModel = departmentService.findPageDepartment(page,limit,departmentQueryVo);

        return Result.ok(pageModel);
    }

    //删除科室接口
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request){
        //获取到传递过来的科室信息。
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //1、获取医院系统传递过来的签名
        String hospSign = (String) paramMap.get("sign");

        //2、根据传递过来的医院编码，查询数据库，查询签名.
        String hoscode = (String)paramMap.get("hoscode");
        String depcode = (String)paramMap.get("depcode");
        String signkey = hospitalSetService.getSignKey(hoscode);

        //3、把数据库查询签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signkey);

        if (!signKeyMd5.equals(hospSign)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        departmentService.removeDepartmentByCode(hoscode,depcode);

        return Result.ok();

    }

    //上传排班
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        //获取到传递过来的科室信息。
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //1、获取医院系统传递过来的签名
        String hospSign = (String) paramMap.get("sign");

        //2、根据传递过来的医院编码，查询数据库，查询签名.
        String hoscode = (String)paramMap.get("hoscode");
        String signkey = hospitalSetService.getSignKey(hoscode);

        //3、把数据库查询签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signkey);

        if (!signKeyMd5.equals(hospSign)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        scheduleService.save(paramMap);
        return Result.ok();
    }

    //查询排班
    @PostMapping("schedule/list")
    public Result findSchedule(HttpServletRequest request) {

        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);


        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String) paramMap.get("limit"));
        String hospSign = (String) paramMap.get("sign");

        String signKey = hospitalSetService.getSignKey(hoscode);

        String signKeyMD5 = MD5.encrypt(signKey);

        if (!signKeyMD5.equals(hospSign)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);

        Page<Schedule> pageModel = scheduleService.findPageSchedule(page,limit,scheduleQueryVo);
        return Result.ok(pageModel);
    }

    //删除排班
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request){
        //获取到传递过来的科室信息。
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //1、获取医院系统传递过来的签名
        String hospSign = (String) paramMap.get("sign");

        //2、根据传递过来的医院编码，查询数据库，查询签名.
        String hoscode = (String)paramMap.get("hoscode");
        String hosScheduleId = (String)paramMap.get("hosScheduleId");
        String signkey = hospitalSetService.getSignKey(hoscode);

        //3、把数据库查询签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signkey);

        if (!signKeyMd5.equals(hospSign)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        scheduleService.removeDepartmentByCode(hoscode,hosScheduleId);

        return Result.ok();
    }

}
